package cn.itsource.pethome.product.controller;

import cn.itsource.pethome.Basic.util.AjaxResult;
import cn.itsource.pethome.Basic.util.PageList;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.domain.ProductDetail;
import cn.itsource.pethome.product.domain.dto.ProductDto;
import cn.itsource.pethome.product.query.ProductQuery;
import cn.itsource.pethome.product.service.IProductDetailService;
import cn.itsource.pethome.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductDetailService productDetailService;

    /**
     * 根据条件查询服务
     * @param query
     * @return
     */
    @PatchMapping
    public PageList<Product> loadProductsByQuery(@RequestBody ProductQuery query){
        return productService.loadAllByQuery(query);
    }

    /**
     * 增加服务
     * @param product
     * @return
     */
    @PutMapping
    public AjaxResult addProduct(@RequestBody Product product){
        try {
            System.out.println(product);
            productService.addProduct(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"增加失败"+e.getMessage());
        }
    }

    /**
     * 删除服务
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult deleteProduct(@PathVariable("id") Long id){
        try {
            productDetailService.delete(id);
            productService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            productDetailService.batchRemove(ids);
            productService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }
    @PatchMapping("/onState")
    public AjaxResult onState(@RequestBody List<Long> ids){
        try {
            productService.onState(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }
    @PatchMapping("/downState")
    public AjaxResult downState(@RequestBody List<Long> ids){
        try {
            productService.downState(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }

    /**
     * 修改服务
     * @param product
     * @return
     */
    @PostMapping
    public AjaxResult updateProduct(@RequestBody Product product){
        try {
            productService.updateProduct(product);
            System.out.println(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }

    }
    /*===============================前台===================================*/

    /**
     * 按照条件查询详情
     * @param productQuery
     * @return
     */
    @PatchMapping("/loadAllByQuery")
    public PageList<Product> loadAllByQuery(@RequestBody ProductQuery productQuery){
        productQuery.setState(1L);
        return productService.loadAllByQuery(productQuery);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @PatchMapping("{id}")
    public Product findById(@PathVariable("id") Long id){
        return productService.findById(id);
    }
}
